<template>
  <div class="login-container">
    <div class="login-box">
      <h2 class="login-title">欢迎登录</h2>
      <el-form ref="loginFormRef" :model="loginForm" :rules="loginRules" label-width="5rem">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="loginForm.username" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            type="password"
            v-model="loginForm.password"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="login">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script lang="ts" setup name="LoginPage">
import type { FormInstance } from 'element-plus'
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()

const loginForm = reactive({
  username: '',
  password: '',
})

const loginRules = {
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
}

const loginFormRef = ref<FormInstance | null>(null)

const login = () => {
  loginFormRef.value?.validate((valid: boolean) => {
    if (valid) {
      // 这里可以添加登录逻辑，例如发送请求到后端
      console.log('登录成功')
      router.push('/')
    } else {
      console.log('error submit!!')
    }
  })
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: flex-end; /* 电脑端靠右 */
  align-items: center;
  height: 100vh;
  background: linear-gradient(to bottom, #87ceeb, #9488e4);
  padding-right: calc(100vw / 15); /* 距离右边三分之一 */
}

/* 手机端居中 */
@media (max-width: 768px) {
  .login-container {
    justify-content: center;
  }
}

.login-box {
  width: 25rem;
  padding: 2rem;
  background-color: rgba(255, 255, 255, 0.877); /* 半透明白色背景 */
  border-radius: 0.3125rem;
  box-shadow: 0 0.125rem 0.75rem 0 rgba(0, 0, 0, 0.2); /* 阴影效果 */
  backdrop-filter: blur(10px); /* 毛玻璃效果 */
}

.login-title {
  text-align: center;
  margin-bottom: 1.25rem;
}
</style>
